// 上传单张图片的函数
function upload(img) {
    return new Promise((resolve, reject) => {
        //模拟上传操作
        setTimeout(() => {
            if (Math.random() < 0.5) {    //假设上传成功
                resolve(`http://example.com/${img}`)
            } else {    //假设上传失败
                rejecte(new Error('Failed'))
            }
        }, 1000)
    })
}

// 上传多张图片
async function uploading(imgs) {
    let res = []    //储存图片结果
    // 并行传输多张图片
    await Promise.all(imgs.map(async img => {
        try {
            //上传图片直至成功
            let url = await retryUpload(img);
            res.push(url)
        } catch (error) {
            console.error(`error`);
        }
    }))
    return res;
}

// 上传图片并进行重试，知道成功
async function retryUpload(img) {
    while (true) {
        try {
            //尝试上传图片
            let url = await upload(img);
            return url;
        } catch (error) {
            // 如果上传失败，则进行重试
            console.error(`Upload failed for ${img}. Retrying...`);
        }
    }
}

// 测试上传一批图片
let imgs = ['img1.jpg', 'img2.jpg', 'img3.jpg'];
uploadImgs(imgs)
    .then(urls => {
        console.log('All images uploaded successfully:');
        console.log(urls); // 输出所有图片的 URL
    })
    .catch(error => {
        console.error('Failed to upload images:', error.message);
    });